#include <dspSimulator/instruction/sth.h>


bool STH::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void STH::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                            std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
}

std::shared_ptr<Instruction> STH::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<STH> sth;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<5>(data, 2, 0b10101) && checkBits<1>(data, 8, 0b0) && existStandardCondition(data)) {
//       STh(.unit) *+baseR[offsetR], dst
            sth = std::make_shared<STH>( dspInstInfo->getAddress(),baseCycle);
            sth->creg_z = extractBits<4>(data, 28);
            sth->y = data[7];
            if(sth->y == 0){
                sth->funcUnit = FuncUnit::D1;
            }else{
                sth->funcUnit = FuncUnit::D2;
            }
            sth->mode = extractBits<4>(data, 9).to_ulong();
            sth->offsetR = extractBits<5>(data, 13).to_ulong();
            sth->cst = sth->offsetR;
            sth->baseR = extractBits<5>(data, 18).to_ulong();
            sth->src1 = extractBits<5>(data, 23).to_ulong();
            sth->s = data[1];
            sth->p = data[0];
            sth->instFormat = InstFormat::Kind1;
        } else if (checkBits<5>(data, 2, 0b01111) && existStandardCondition(data)) {
            sth = std::make_shared<STH>( dspInstInfo->getAddress(),baseCycle);
            sth->creg_z = extractBits<4>(data, 28);
            sth->y = data[7];
            sth->funcUnit = FuncUnit::D2;
            sth->s = data[1];
            sth->cst = extractBits<15>(data, 8).to_ulong();
            sth->dst = extractBits<5>(data, 23).to_ulong();
            sth->p = data[0];
            sth->instFormat = InstFormat::Kind2;
        }
    }
    return sth;
}

void STH::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {
    if (executePhase == ExecutePhase::E1) {
        auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
        if (instFormat == InstFormat::Kind1) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            m_visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 1, dspMachine);
        } else if (instFormat == InstFormat::Kind2) {
            u32 *baseRPtr;
            if (y == 0) {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName("B14")->getDataPtr();
            } else {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName("B15")->getDataPtr();
            }
            m_visitMemAddr = *baseRPtr + (cst << 1);
        }
        m_data = *src1LoRegPtr;
    } else if (executePhase == ExecutePhase::E3) {
        targetMachine->getMMU()->getMemory()->writeNBits<16>(m_visitMemAddr, {m_data});
    }
}

std::shared_ptr<DspInstruction> STH::clone() {
    return std::make_shared<STH>(*this);
}

std::string STH::toString() const{
    std::string res;
    if (isCompactInstruction) {

    } else {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)];
    if (instFormat == InstFormat::Kind1) {
        res += " " + getSrc1Reg32Name() + ",*" + addressModeToString();
    } else if (instFormat == InstFormat::Kind2) {
        res += " " + getSrc1Reg32Name() + ",*+";
        if (y == 0)
            res += "B14[" + std::to_string(cst) + "]";
        else
            res += "B15[" + std::to_string(cst) + "]";
    }
    return res;
}

